package com.facesteel.speech_play.utils;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import java.io.File;

public class StorageUtils {

    public static String getStoragePath(Context context, String type) {
        try {
            // 1. 检查外部存储状态
            String externalStorageState = Environment.getExternalStorageState();
            File dir;

            if (Environment.MEDIA_MOUNTED.equals(externalStorageState)) {
                // 外部存储可用
                dir = context.getExternalFilesDir(type);
            } else {
                // 回退到内部存储
                dir = context.getFilesDir();
            }

            // 2. 确保目录存在
            if (dir != null && dir.exists()) {
                return dir.getAbsolutePath();
            } else {
                Log.e("TAG", "存储路径不可用");
                return null;
            }
        } catch (SecurityException e) {
            // 处理旧版本权限问题
            Log.e("TAG", "需要存储权限", e);
            return null;
        }
    }

    // 简化版（无 type 参数）
    public static String getStoragePath(Context context) {
        return getStoragePath(context, null);
    }
}

//// 使用示例
//String path = StorageUtils.getStoragePath(context);
//String downloadPath = StorageUtils.getStoragePath(context, Environment.DIRECTORY_DOWNLOADS);